package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sizu.annotation.Log;
import com.sizu.common.dto.*;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TPurchaseList;
import com.sizu.entity.TPurchaseListGoods;
import com.sizu.entity.TPurchaseListZyx;
import com.sizu.entity.TSupplier;
import com.sizu.service.SyncService;
import com.sizu.service.impl.TPurchaseListGoodsServiceImpl;
import com.sizu.service.impl.TPurchaseListServiceImpl;
import com.sizu.service.impl.TSupplierServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-21
 */
@RestController
@RequestMapping("/purchase/tPurchaseList")
public class TPurchaseListController {

    @Autowired
    private TPurchaseListServiceImpl tPurchaseListServiceImpl;

    @Autowired
    private TPurchaseListGoodsServiceImpl tPurchaseListGoodsServiceImpl;

    @Autowired
    private TSupplierServiceImpl tSupplierServiceImpl;

    @Autowired
    private SyncService syncService;

    //根据进货单号查询出所有的订单
    @RequestMapping("/list")
    public ResultEntity list(TPurchaseListZyxDto tPurchaseListZyxDto){
        TPurchaseListZyx tPurchaseListZyx=new TPurchaseListZyx();
        BeanUtils.copyProperties(tPurchaseListZyxDto,tPurchaseListZyx);
        List<TPurchaseListZyx> tPurchaseLists = tPurchaseListServiceImpl.getPurchaseListZyx(tPurchaseListZyx);
        return ResultEntity.ok(tPurchaseLists);
    }

    //查询出所有的进货订单列表
    @RequestMapping("/list1")
    public ResultEntity list1(){
        List<TPurchaseList> list = tPurchaseListServiceImpl.selectList1();
        return ResultEntity.ok(list);
    }

    //查询出所有的供应商
    @RequestMapping("/supplierList")
    public ResultEntity supplierList(SupplierDto supplierDto){
        Wrapper<TSupplier> wrapper = new EntityWrapper<TSupplier>();
        wrapper.ne("enable",0);
        List<TSupplier> tSuppliers = tSupplierServiceImpl.selectList(wrapper);
        return ResultEntity.ok(tSuppliers);
    }

    //查询出所有的单号
    @RequestMapping("/purchaseList")
    public ResultEntity supplierList(PurchaseListDto purchaseListDto){
        Wrapper<TPurchaseList> wrapper = new EntityWrapper<TPurchaseList>();
        List<TPurchaseList> tPurchaseList = tPurchaseListServiceImpl.selectList(wrapper);
        return ResultEntity.ok(tPurchaseList);
    }



    @Log("当点击添加时，先查询出所有的进货商品")
    //当点击添加时，先查询出所有的进货商品
    @RequestMapping("/purchaseGoodsList")
    public ResultEntity purchaseGoodsList(PurchaseListGoodsDto purchaseListGoodsDto){
        Wrapper<TPurchaseListGoods> wrapper = new EntityWrapper<TPurchaseListGoods>();
        List<TPurchaseListGoods> tSuppliers = tPurchaseListGoodsServiceImpl.selectList(wrapper);
        return ResultEntity.ok(tSuppliers);
    }

    //点击添加商品时，查询出商品的所有信息
    @RequestMapping("/purchaseGoodsList1")
    public ResultEntity purchaseGoodsList1(){
        List<TPurchaseListGoods> list = tPurchaseListGoodsServiceImpl.selectGoodsList();
        return ResultEntity.ok(list);
    }


    @Log("根据id查找商品的信息")
    //根据id查找商品的信息
    @RequestMapping("/purchaseGoodsOne")
    public ResultEntity purchaseGoodsOne(Integer id){
        PpGDto p = tPurchaseListGoodsServiceImpl.purchaseGoodsOne(id);
        return ResultEntity.ok(p);
    }


    @Log("做订单和订单详情添加的方法")
    //做订单和订单详情添加的方法
    @RequestMapping("/purchaseAdd")
    public ResultEntity purchaseAdd(PurchaseListDto purchaseListDto){

        TPurchaseList tPurchaseList = new TPurchaseList();
        BeanUtils.copyProperties(purchaseListDto,tPurchaseList);
        tPurchaseListServiceImpl.insert(tPurchaseList);//向订单表里添加数据
        //获取新添加进去的id
        purchaseListDto.setId(tPurchaseList.getId());
        syncService.syncPurchase(purchaseListDto);
        if(null!=purchaseListDto.getPurchaseListGoodsJSON()){
            List<TPurchaseListGoods> tPurchaseListGoods = JSON.parseArray(purchaseListDto.getPurchaseListGoodsJSON(), TPurchaseListGoods.class);
            for (TPurchaseListGoods tPurchaseListGood : tPurchaseListGoods) {
                tPurchaseListGood.setPurchaseListId(tPurchaseList.getId());
                tPurchaseListGoodsServiceImpl.insert(tPurchaseListGood);
            }
        }
        return ResultEntity.ok();
    }

    /**
     * 返回随机编号
     * @return
     */
    @RequestMapping("/number")
    public ResultEntity getOrderNumber(){
        return ResultEntity.ok("获取成功","JH"+System.currentTimeMillis());
    }


}
